"""筛选"""

from src.db import dbs, dbm


class Filter:
    """使用链式调用的方式，支持一次筛选多个"""

    def __init__(self) -> None:
        self.apps, self.apps_Java, self.apps_Web = [], [], []
        db_session_Java = dbm.get("hosting_Java")
        with dbm.get("hosting_Java") as db_session_Java:
            from sqlalchemy.orm import scoped_session
            db_session_Java: scoped_session
            for i in db_session_Java.query(dbs.hosting_Java.App).all():
                self.apps.append(i)
                self.apps_Java.append(i)
        with dbm.get("hosting_Static") as db_session_Web:
            from sqlalchemy.orm import scoped_session
            db_session_Web: scoped_session
            for i in db_session_Web.query(dbs.hosting_Static.App).all():
                self.apps.append(i)
                self.apps_Web.append(i)
            self.result = []

    def node(self, v: str):
        """所属节点筛选"""
        result = []
        for i in self.apps:
            if i.node == v:
                result.append(i)
        self.apps = result
        return self

    def stype(self, v: str):
        """应用类型筛选"""
        result = []
        for i in getattr(self, f"apps_{v}"):
            for j in self.apps:
                if i == j:
                    result.append(j)
        self.apps = result
        return self

    def keepalive(self, v: bool):
        """保活状态筛选"""
        result = []
        t = "0" if v else "1"
        for i in self.apps:
            if i.keepalive == t:
                result.append(i)
        self.apps = result
        return self

    def status(self, v: str):
        """运行状态筛选"""
        result = []
        for i in self.apps:
            if i == v:
                result.append(i)
        self.apps = result
        return self

    def update_time(self, v: str):
        """更新时间筛选"""
        pass

    def register_time(self, v: str):
        """注册时间筛选"""
        pass

    def alias(self, v: str):
        """别名筛选"""
        result = []
        for i in self.apps:
            if i == v:
                result.append(i)
        self.apps = result
        return self
